package top.lyjwn.todo.weinxin.api.applet;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.lyjwn.todo.common.utils.http.HttpUtil;
import top.lyjwn.todo.weinxin.api.url.WXAppletUrl;
import top.lyjwn.todo.weinxin.applet.AppletAccessToken;
import top.lyjwn.todo.weinxin.param.WXAppletParam;
import top.lyjwn.todo.weinxin.param.WXBjnewsParam;

@Component
public class SendMessage {


    @Autowired
    private WXAppletUrl wxAppletUrl;

    public SendMessage() {
    }


    public String getUrl(AppletAccessToken accessToken) {
        return wxAppletUrl.getSEND_MESSAGE().replace("#{ACCESS_TOKEN}", accessToken.getAccess_token());
    }

    public enum MiniprogramState {
        developer,
        trial,
        formal;

    }

    public enum Lang {

        zh_CN,
        en_US,
        zh_HK,
        zh_TW
    }


    public String sendMpTemplateMsg(AppletAccessToken accessToken, String openid, String template_id, String pagepath, MiniprogramState miniprogram_state, Lang lang, JSONObject data) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", openid);
        jsonObject.put("template_id", template_id);
        jsonObject.put("page", pagepath);
        jsonObject.put("data", data);
        jsonObject.put("miniprogram_state", miniprogram_state.name());
        jsonObject.put("lang", lang.name());
        String jsonString = jsonObject.toJSONString();
        String request = HttpUtil.request(getUrl(accessToken), HttpUtil.METHOD_POST, jsonString);
        return request;
    }

    public String sendMpTemplateMsg(AppletAccessToken accessToken, String openid, String template_id, String pagepath, MiniprogramState miniprogram_state, JSONObject data) {
        String request = this.sendMpTemplateMsg(accessToken, openid, template_id, pagepath, miniprogram_state, Lang.zh_CN, data);
        return request;
    }
}
